#include "communication-device-electrical.h"
#include "smart-device-database.h"

CommunicationDeviceElectrical::CommunicationDeviceElectrical(SmartDeviceManager *manager) : smartDeviceManger(manager)
{
    handle["add"] = &CommunicationDeviceElectrical::add;
    handle["remove"] = &CommunicationDeviceElectrical::remove;
    handle["open"] = &CommunicationDeviceElectrical::open;
    handle["close"] = &CommunicationDeviceElectrical::close;
    handle["query"] = &CommunicationDeviceElectrical::query;
}

void CommunicationDeviceElectrical::communication(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    auto func = handle[command];
    if (!func)
    {
        return;
    }
    (this->*func)(netprotocol, netobj);
}

void CommunicationDeviceElectrical::query(SmartDeviceNetProtocol &netprotocol,
                                          NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    uint32_t deviceType = netprotocol.getDeviceType();
    uint32_t deviceNumber = netprotocol.getDeviceNumber();
    uint8_t lockStatu;
    bool ok = smartDeviceManger->queryElectrical(deviceType, deviceNumber, lockStatu);
    string errmsg = ok ? "" : "error";
    string data = lockStatu ? "1" : "0";
    replay(netobj, command, errmsg, data);

    return;
}

void CommunicationDeviceElectrical::add(SmartDeviceNetProtocol &netProtocol,
                                        NetCommunication *netObj)
{
    SmartDeviceDatabase *smartDeviceDatabase = SmartDeviceDatabase::getSmartDeviceDatabase();
    bool ok = smartDeviceDatabase->addDevice(
        netProtocol.getOperation(),
        netProtocol.getDeviceValue("deviceType"),
        netProtocol.getDeviceValue("deviceNumber"));

    string errmsg = ok ? "" : "error";
    // 等待锁那边回复 UDP发送
    replay(netObj, netProtocol.getCommand(), errmsg);
}

void CommunicationDeviceElectrical::remove(SmartDeviceNetProtocol &netProtocol,
                                           NetCommunication *netObj)
{
    SmartDeviceDatabase *smartDeviceDatabase = SmartDeviceDatabase::getSmartDeviceDatabase();
    bool ok = smartDeviceDatabase->removeDevice(
        netProtocol.getOperation(),
        netProtocol.getDeviceValue("deviceType"),
        netProtocol.getDeviceValue("deviceNumber"));

    string errmsg = ok ? "" : "error";
    // 等待锁那边回复 UDP发送
    replay(netObj, netProtocol.getCommand(), errmsg);
}

void CommunicationDeviceElectrical::open(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    uint32_t deviceType = netprotocol.getDeviceType();
    uint32_t deviceNumber = netprotocol.getDeviceNumber();
    // 跟uart I2c spi 通讯去了
    bool ok = smartDeviceManger->openElectrical(deviceType, deviceNumber);
    // 2返回门锁控制结果 网络通讯协议返回给QT层
    string errmsg = ok ? "" : "error";
    replay(netobj, command, errmsg);
    return;
}

void CommunicationDeviceElectrical::close(SmartDeviceNetProtocol &netprotocol, NetCommunication *netobj)
{
    string command = netprotocol.getCommand();
    uint32_t deviceType = netprotocol.getDeviceType();
    uint32_t deviceNumber = netprotocol.getDeviceNumber();
    // 跟uart I2c spi 通讯去了
    bool ok = smartDeviceManger->closeElectrical(deviceType, deviceNumber);
    // 2返回门锁控制结果 网络通讯协议返回给QT层
    string errmsg = ok ? "" : "error";
    replay(netobj, command, errmsg);
    return;
}

void CommunicationDeviceElectrical::replay(NetCommunication *netobj,
                                           const string &command,
                                           const string &errmsg,
                                           const string &data)
{

    SmartDeviceNetProtocol netProtocol;
    netProtocol.addCommand("electrical");
    netProtocol.addCommand(command);
    netProtocol.addResult(errmsg, data);

    string replyJsonData = netProtocol.toString();
    cout << "打印回复的json字符串" << replyJsonData << endl;
    // string 转  vector
    vector<uint8_t> packet;
    packet.assign(replyJsonData.begin(), replyJsonData.end());
    bool ret = netobj->sendPacket(packet);
    if (ret != true)
    {
        fprintf(stderr, "Fail to sendPacket \n");
        return;
    }

    return;
}
